package hu.myandroidlife.util;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;

//Activity任务管理器类
public class ActivityCollector {
	public static List<Activity> activities = new ArrayList<Activity>();

	// 将当前activity添加到activity任务管理器中
	public static void addActivity(Activity activity) {
		activities.add(activity);
	}

	// 将当前activity从activity任务管理器中删除
	public static void removeActivity(Activity activity) {
		activities.remove(activity);
	}

	// 将所有的activity从activity任务管理器中删除(即可实现一键退出程序)
	public static void removeAllActivity() {
		for (Activity activity : activities) {
			if (activity != null) {
				activity.finish();// 结束
			}
		}
	}

	// 从Activity集合查询, 传入的Activity是否存在 如果存在就返回该Activity,不存在就返回null
	public static Activity getActivity(Class<?> activity) {
		for (int i = 0; i < activities.size(); i++) {
			// 判断是否是自身或者子类
			if (activities.get(i).getClass().isAssignableFrom(activity)) {
				return activities.get(i);
			}
		}
		return null;
	}
}
